//
// Created by dgy on 2022/11/11.
//
#include "server.h"
#include "player.h"

int initial = 0;
int researchButton = 1;

typedef  struct Informations
{
    /* data */
    int hitNum;
    int optionS;
    int optionH;
    int last_Hit_Index[2];
    int found[10][10];

}Information;







Battlefield PlayerA_BF;
char * PlayerAName = "Donggenyang";



// gffff gfdgdgfgd

// 玩家部署舰艇
int DeploymentA()
{

    for(int i=0;i<M;i++)
        for(int j=0;j<M;j++)
        {
            PlayerA_BF.area[i][j] = 1;
        }
    // add your arrangement, for example:
    PlayerA_BF.area[9][2] = D; PlayerA_BF.area[9][3] = D;
    PlayerA_BF.area[5][9] = S; PlayerA_BF.area[6][9] = S; PlayerA_BF.area[7][9] = S;
    PlayerA_BF.area[7][6] = C; PlayerA_BF.area[8][6] = C; PlayerA_BF.area[9][6] = C;
    PlayerA_BF.area[5][0] = B; PlayerA_BF.area[6][0] = B; PlayerA_BF.area[7][0] = B; PlayerA_BF.area[8][0] = B;
    PlayerA_BF.area[0][3] = A; PlayerA_BF.area[1][3] = A; PlayerA_BF.area[2][3] = A; PlayerA_BF.area[3][3] = A; PlayerA_BF.area[4][3] = A;

    printBF(&PlayerA_BF);
    return 0;
}

int* getAttack_Object_Index()
{
    int* attack;
    return attack;
}
// 根据上次是否击中确定策略，选择下一个攻击点
static int lasti = 4;
static int lastj = 4;
//初始化矩阵，记录矩阵的情况
static int checkMaritx[10][10]={0};
//步长记录这是第几步
static int step=1;
int StrategyA() {
    int i, j;
    // 从服务器端获取上次攻击是否击中信息
    int isHit = ALastHitB();
    i = lasti;
    j = lastj;
    //未击中的情况
    if (isHit == 0) {
        i=rand()%M;
        j=rand()%M;
        while (checkMaritx[i][j]==1){
            i=rand()%M;
            j=rand()%M;
        }
    } else {
        /*
         * 击中后，有三种情况需要处理
         * 1.在中间区域，要找他周围的四个位置
         * 2.在四个角上，要找他周围的两个位置
         * 3.在边缘，要找他周围的三个位置
         * */
        if (i >= 1 && i < M - 1 && j >= 1 && j < M - 1) {
            if (checkMaritx[i + 1][j] == 0)
                i = lasti + 1;
            else if(checkMaritx[i-1][j]==0)
                i = lasti - 1;
            else if (checkMaritx[i][j + 1] == 0)
                j = lastj + 1;
            else if (checkMaritx[i][j - 1] == 0)
                j = lastj - 1;
        } else if ((i == 0 && j == 0) || (i == 0 && j == M - 1) || (i == M - 1 && j == 0) ||
                   (i == M - 1 && j == M - 1)) {
            if (i == 0 && j == 0) {
                if (checkMaritx[i][j + 1] == 0)
                    j = lastj + 1;
                else if (checkMaritx[i + 1][j] == 0)
                    i = lasti + 1;
            } else if (i == 0 && j == M - 1) {
                if (checkMaritx[i][j - 1] == 0)
                    j = lastj - 1;
                else if (checkMaritx[i + 1][j] == 0)
                    i = lasti + 1;
            } else if (i == M - 1 && j == 0) {
                if (checkMaritx[i][j + 1] == 0)
                    j = lastj + 1;
                else if (checkMaritx[i - 1][j] == 0)
                    i = lasti - 1;
            } else if (i == M - 1 && j == M - 1) {
                if (checkMaritx[i][j - 1] == 0)
                    j = lastj - 1;
                else if (checkMaritx[i - 1][j] == 0)
                    i = lasti - 1;
            }
        } else {
            if (i == 0 || i == M - 1) {
                if (checkMaritx[i][j + 1] == 0)
                    j = lastj + 1;
                else if (checkMaritx[i][j - 1] == 0)
                    j = lastj - 1;
                else if (i == 0 && checkMaritx[i + 1][j] == 0)
                    i = lasti + 1;
                else if (i == M - 1 && checkMaritx[i - 1][j] == 0)
                    i = lasti - 1;
            } else if (j == 0 || j == M - 1) {
                if (checkMaritx[i - 1][j] == 0)
                    i = lasti - 1;
                else if (checkMaritx[i + 1][j] == 0)
                    i = lasti + 1;
                else if (j == 0 && checkMaritx[i][j + 1] == 0)
                    j = lastj + 1;
                else if (j == M - 1 && checkMaritx[i][j - 1] == 0)
                    j = lastj - 1;
            }
        }
    }
    //记录上一次的情况
    lasti = i;
    lastj = j;
    checkMaritx[i][j] = 1;
    firetoB(i, j);
    return 0;
}
